#include "ServerTimeMgr.h"

#include <core/time/system_time.h>
#include <core/time/time.h>

ServerTimeMgr& ServerTimeMgr::instance() {
  static ServerTimeMgr s_tm;
  return s_tm;
}

time_t ServerTimeMgr::serverTime() { return time(0) - m_time_delta; }

void ServerTimeMgr::setServerTime(const time_t& serverTime) {
  m_time_delta = time(0) - serverTime;
  bool bDayChange = false;
  qb::base::CTime ctCur(serverTime);
  qb::base::CTime ctPrev(m_prev_time);
  if (ctCur.GetYear() != ctPrev.GetYear() ||
      ctCur.GetMonth() != ctPrev.GetMonth() ||
      ctCur.GetDay() != ctPrev.GetDay())
    bDayChange = true;

  m_prev_time = serverTime;
}

unsigned int ServerTimeMgr::today() {
  time_t t = serverTime();
  if (t == 0) t = time(0);
  tm tmToday = qb::base::time2tm(t);
  unsigned int nToday = (tmToday.tm_year + 1900) * 10000 +
                        (tmToday.tm_mon + 1) * 100 + tmToday.tm_mday;
  return nToday;
}